interface ClassA<T>{
    length: number ;
    name:string;
    age:T;
}

class ClassC implements ClassA<number>{

    constructor(public length:number,public name:string){
        this.length = length
        this.name = name ?? "nihao"

        this.printName()
    }

    age: number = 0;

    printName(){
        console.log("name:" + this.name + "  age:" + this.age)
    }
}

/**
 * 泛型约束：ClassB<T extends ClassA>
 * 泛型函数：print<V>(a:V)
 */
class ClassB<T extends ClassA<number>>{
    // T 必须含length变量
    public comp<T extends { length: number }>(a: T, b: T) {
        if (a.length >= b.length) {
          return a;
        }
        return b;
    }

    public print<V>(a:V){
        console.log(a)
    }

    // 类型参数的默认值
    public getFirst<T = string>(
        arr:T[]
      ):T {
        return arr[0];
      }

}

var classB = new ClassB<ClassC>();
let c1 = new ClassC(12, "xiaowu")
let c2 = new ClassC(13, "xiaowang")

c1.age = 10
c1.length = 20
c1.name = "zhangsan"
console.log("c1.age:" + c1.age)
console.log(classB.comp(c1, c2))
classB.print(classB.getFirst<number>([122, 2, 3]))






